package com.woniu.controller;

import com.woniu.entity.Customer;
import com.woniu.service.CustomerService;
import com.woniu.util.ResponseData;
import com.woniu.util.ResponseEnum;
import com.woniu.vo.CustomerVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * FileName: CustomerController
 * Author: wujiajun
 * Date:   2025/2/15 16:50
 * Description:DOTO
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;

    @GetMapping("/list")
    public ResponseData<?> getCustomerList(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        List<CustomerVO> customers = customerService.getCustomerList(keyword, pageNum, pageSize);
        return ResponseData.ok(customers);
    }

    @PostMapping("/add")
    public ResponseData<?> addCustomer(@RequestBody Customer customer) {
        boolean success = customerService.addCustomer(customer);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }

    @DeleteMapping("/{id}")
    public ResponseData<?> deleteCustomer(@PathVariable("id") Long id) {
        boolean success = customerService.deleteCustomer(id);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }

    @PostMapping("/update")  // 改为 POST 方法
    public ResponseData<?> updateCustomer(@RequestBody Customer customer) {
        boolean success = customerService.updateCustomer(customer);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }
}
